﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using BLL;
using Model;

namespace PMS.Web
{
    /// <summary>
    /// UserMaintain 的摘要说明
    /// </summary>
    public class UserMaintain : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strAction = context.Request["action"];
            string strReturn = "";
            if (!string.IsNullOrEmpty(strAction))
            {
                System.Reflection.MethodInfo minfo = this.GetType().GetMethod(strAction);
                if (minfo != null)
                {
                    object o = minfo.Invoke(this, new object[] { context });
                    if (o != null)
                        strReturn = o.ToString();
                }
            }
            context.Response.Write(strReturn);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string DeleteUser(HttpContext context)
        {
            string roleID = context.Request["id"];
            StringBuilder strHtml = new StringBuilder();
            PMS_UserBLL bll = new PMS_UserBLL();
            if (bll.Delete(Convert.ToInt32(roleID)))
            {
                return "0";
            }
            else
            {
                return "-1";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public string GetList(HttpContext context)
        {
            string roleID = context.Request["roleID"];
            if (!string.IsNullOrEmpty(roleID))
            {
                PMS_UserBLL bll = new PMS_UserBLL();
                List<PMS_User> list = bll.GetList(Convert.ToInt32(roleID));
                StringBuilder strHtml = new StringBuilder();
                strHtml.Append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><th class=\"GridHeader\" width=\"8%\" align=\"center\">序号</th><th class=\"GridHeader\" width=\"12%\">用户名</th><th class=\"GridHeader\" width=\"7%\">角色</th><th class=\"GridHeader\" width=\"7%\">密码</th><th class=\"GridHeader\" width=\"4%\">编辑</th><th class=\"GridHeader\" width=\"5%\">删除</th></tr>");
                int count = 0;
                foreach (PMS_User m in list)
                {
                    count++;
                    strHtml.Append("<tr><td class=\"GridTableTD\" align=\"center\">" + count + "</td><td class=\"GridTableTD\">" + m.loginName + "</td><td class=\"GridTableTD\">" + m.role + "</td><td class=\"GridTableTD\">" + m.loginPassword + "</td><td class=\"GridTableTD\" style=\"cursor:pointer;\" onclick=\"editUser(" + m.id + ")\">编辑</td><td class=\"GridTableTD\" style=\"cursor:pointer;\" onclick=\"deleteUser(" + m.id + ")\">删除</td></tr>");
                }
                if (list == null || list.Count <= 0)
                {
                    strHtml.Append("<tr><td colspan=\"6\" class=\"GridTableTD\" align=\"center\">没有数据</td></tr>");
                }
                strHtml.Append("</table>");
                return strHtml.ToString();
            }
            else
            {
                return "";
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}